import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Menu from '../components/Menu.vue'
// 这是一个空的框架，用来存放子级菜单
import Layout from '../content/Layout.vue'

Vue.use(VueRouter)

const routes = [
  // 这是登录页
  {
    path: '/',
    name: 'Login',
    component: Login,
    redirect: '/login',
    children: [
      { path: '/login', component: Login }
    ]
  },
  // 这是首页
  // children是右侧区域显示的页面
  {
    path: '/menu',
    name: 'Menu',
    component: Menu,
    redirect: '/home',
    meta: { title: '首页' },
    children: [
      {
        path: '/home',
        name: 'Home',
        meta: { title: '首页' },
        component: () => import(/* webpackChunkName: "about" */ '../components/Home.vue')
      },
      {
        path: '/task',
        name: 'Task',
        meta: { title: '神仙管理' },
        component: () => import(/* webpackChunkName: "task" */ '../components/Task.vue')
      },
      {
        path: '/task/taskdetails',
        name: 'TaskDetails',
        meta: { title: '详情' },
        component: () => import(/* webpackChunkName: "task" */ '../page/TaskDetails.vue')
      },
      {
        path: '/wish',
        name: 'Wish',
        meta: { title: '祈愿管理' },
        component: () => import(/* webpackChunkName: "task" */ '../components/Wish.vue')
      },
      {
        path: '/money',
        name: 'Money',
        meta: { title: '货币管理' },
        component: () => import(/* webpackChunkName: "task" */ '../components/Money.vue')
      },
      {
        path: '/log',
        name: 'Log',
        meta: { title: '日志管理' },
        component: () => import(/* webpackChunkName: "task" */ '../components/Log.vue')
      },
      {
        path: '/content',
        name: 'Layout',
        component: Layout,
        meta: { title: '内容管理' },
        redirect: '/content/article',
        children: [
          {
            path: '/content/article',
            name: 'Article',
            meta: { title: '文章' },
            component: () => import(/* webpackChunkName: "content" */ '../content/Article.vue')
          },
          {
            path: '/content/article/:id',
            name: 'ArticleEdit',
            meta: { title: '编辑' },
            // 是否隐藏
            hidden: true,
            component: () => import(/* webpackChuinkName: "content" */ '../content/ArticleEdit.vue')
          },
          {
            path: '/content/category',
            name: 'Category',
            meta: { title: '分类' },
            component: () => import(/* webpackChunkName: "content" */ '../content/Category.vue')
          }
        ]
      },
      {
        path: '/high-level/big-data',
        name: 'BigData',
        meta: { title: '乾坤镜' },
        component: () => import(/* webpackChunkName: "data" */ '../components/BigData.vue')
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

// 路由导航守卫：从哪里来到哪里去是否放行
router.beforeEach((to, form, next) => {
  if (to.path === '/login') return next()
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})

export default router
